package com.coolrui.controller;

import cn.hutool.json.JSONUtil;
import com.coolrui.common.enums.ResultCodeEnum;
import com.coolrui.common.exception.ParamException;
import com.coolrui.common.system.ApiList;
import com.coolrui.common.system.ApiResult;
import com.coolrui.common.system.Response;
import com.coolrui.converter.OrderConverter;
import com.coolrui.domain.order.entity.UserOrder;
import com.coolrui.domain.order.service.UserOrderHandleService;
import com.coolrui.model.UserOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理
 * @author coolrui
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private UserOrderHandleService orderHandleService;
    @Autowired
    private OrderConverter orderConverter;

    /**
     * 批量创建测试订单
     * @param userId 用户ID
     * @param orderNum 创建订单数量
     * @return 订单列表
     */
    @PostMapping("createMockOrder")
    public ApiResult<List<UserOrderVO>> createMockOrder(@RequestParam Long userId, @RequestParam(defaultValue = "1") Integer orderNum) {
        try {
            //创建模拟订单
            List<UserOrder> orderList = orderHandleService.createMockOrder(userId,orderNum);
            //判断是否创建成功
            if (orderList.isEmpty()) {
                return Response.makeErrRsp("创建订单失败");
            }
            //转换返回VO实体
            List<UserOrderVO> orderVOList = orderConverter.toUserOrderVO(orderList);

            log.info("创建{}个测试订单，userId：{}, orderList：{}", orderNum,userId,JSONUtil.toJsonStr(orderVOList));
            return Response.makeOKRsp(orderVOList);
        } catch (ParamException e) {
            log.error("创建订单-参数错误", e);
            return Response.makeErrRsp(ResultCodeEnum.VALID, e.getMessage());
        } catch (Exception e) {
            log.error("创建订单-发生错误", e);
            return Response.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 查询订单详情
     * @param orderId 订单ID
     * @return 订单实体
     */
    @PostMapping("getOrder")
    public ApiResult<UserOrderVO> getOrder(@RequestParam Long orderId) {
        UserOrder userOrder = orderHandleService.getOrder(orderId);
        //转换返回VO实体
        UserOrderVO orderVO = orderConverter.toUserOrderVO(userOrder);
        log.info("查询订单详情，orderId:{} , orderVO:{}", orderId, JSONUtil.toJsonStr(orderVO));
        return Response.makeOKRsp(orderVO);
    }

    /**
     * 查询用户订单列表
     * @param page 页码
     * @param pageSize 每页条数
     * @param userId 用户ID
     * @return 订单列表
     */
    @GetMapping("getOrderList")
    public ApiResult<ApiList<UserOrderVO>> getOrderList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "0") Long userId) {
        ApiList<UserOrder> orderList = orderHandleService.getOrderList(userId, page, pageSize);
        //转换返回VO实体
        List<UserOrderVO> orderVOList = orderConverter.toUserOrderVO(orderList.getList());
        log.info("查询用户订单列表，userId：{}, orderList：{}", userId, JSONUtil.toJsonStr(orderVOList));
        return Response.makeOKRsp(ApiList.makeResult(orderList.getHasMore(), orderVOList));
    }

    /**
     * 支付订单
     * @param orderId 订单ID
     * @return 订单实体
     */
    @PostMapping("payOrder")
    public ApiResult<UserOrderVO> payOrder(@RequestParam Long orderId) {
        try {
            UserOrder userOrder = orderHandleService.payOrder(orderId);
            //转换返回VO实体
            UserOrderVO orderVO = orderConverter.toUserOrderVO(userOrder);
            log.info("支付订单成功，orderId:{}, orderVO:{}", orderId, JSONUtil.toJsonStr(orderVO));
            return Response.makeOKRsp(orderVO);
        } catch (Exception e) {
            return Response.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 取消订单
     * @param orderId 订单ID
     * @return 订单实体
     */
    @PostMapping("cancelOrder")
    public ApiResult<UserOrderVO> cancelOrder(@RequestParam Long orderId) {
        try {
            UserOrder userOrder = orderHandleService.cancelOrder(orderId);
            //转换返回VO实体
            UserOrderVO orderVO = orderConverter.toUserOrderVO(userOrder);
            log.info("取消订单成功，orderId:{}, orderVO:{}", orderId, JSONUtil.toJsonStr(orderVO));
            return Response.makeOKRsp(orderVO);
        } catch (Exception e) {
            return Response.makeErrRsp(e.getMessage());
        }
    }
}